Scenario C - Peak Number Variation

In this scenario the number of peaks in a generated dataset is varied in from low to high (e.g. 2-6), the rest of the parameters is kept constant (noise level = 1%). The number of peaks expected by the probabilistic model is varied between the low and high peak number.

The model used in the inference of the parameters is formulated as follows:

\begin{equation} \large y = f(x) = \sum\limits_{m=1}^M \big[A_m \cdot e^{-\frac{(x-\mu_m)^2}{2\cdot\sigma_m^2}}\big] + \epsilon \end{equation}
In [1]:
%matplotlib inline
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import pymc3 as pm
import arviz as az

#az.style.use('arviz-darkgrid')

print('Running on PyMC3 v{}'.format(pm.__version__))
WARNING (theano.tensor.blas): Using NumPy C-API based implementation for BLAS functions.
Running on PyMC3 v3.8

Import local modules

In [2]:
import os
import sys
sys.path.append('../../modules')
import datagen as dg
import models as mdl
import results as res
import figures as fig
import settings as cnf

Local configuration

In [3]:
# output for results and images
out_path      = './output_peaks_4x4'
file_basename = out_path + '/scenario_peaks'
        
# if dir does not exist, create it
if not os.path.exists(out_path):
    os.makedirs(out_path)

conf = {}
    
# scenario name
conf['scenario'] = 'peak variation'
    
# initialization method for sampler
conf['init_mode'] = 'adapt_diag'

# probabilistic model (priors)
conf['prior_model'] = 'lognormal'

# provide peak positions to the model as testvalues ('yes'/'no')
conf['peak_info'] = 'yes'

# model mode ('train'/eval')
conf['model_mode'] = 'train'

# data mode ('generate'/'preload')
conf['data_mode'] = 'generate'

# dataset directory (needed for 'preload' data mode)
#conf['dataset_dir'] = './input_datasets'

# number of cores to run sampling chains on
conf['ncores'] = 2

# number of samples per chain
conf['nsamples'] = 2000
In [4]:
conf
Out[4]:
{'scenario': 'peak variation',
 'init_mode': 'adapt_diag',
 'prior_model': 'lognormal',
 'peak_info': 'yes',
 'model_mode': 'train',
 'data_mode': 'generate',
 'ncores': 2,
 'nsamples': 2000}

Save configuration

In [5]:
cnf.save(out_path, conf)

Generate data and plot

In [6]:
# list of wavelengths (x-values)
xval = [i for i in range(200, 400, 2)]

ldata  = []
lpeaks = []
lpeakdata = []

# number of spectra per peak number
nsets  = 4

# number of peaks in the spectrum
peak_numbers = [2,3,4,5]

# total number of datasets
tsets = nsets * len(peak_numbers)
        
if conf['model_mode'] == 'train' and conf['data_mode'] == 'generate':
    # generate the datasets
    for pn in peak_numbers:
        for i in range(nsets):
            df, peaks, df_peakinfo = dg.data_generator(xvalues=xval, nsamples=15, npeaks=pn)
            ldata.append(df)
            lpeaks.append(peaks)
            lpeakdata.append(df_peakinfo)
    # save data and peak information to disk
    for i in range(len(ldata)):
        ldata[i].to_csv(out_path + '/dataset_%02d.csv' % (i+1), index=False)
        lpeakdata[i].to_csv(out_path + '/peakdata_%02d.csv' % (i+1), index=False)
    dg.data_save(out_path + '/peakinfo.csv', lpeaks)
        
elif conf['model_mode'] == 'train' and conf['data_mode'] == 'preload':           
    # load pre-generated datasets from disk
    ldata, lpeaks, lpeakdata = dg.data_load(tsets, conf['dataset_dir'])
    
else:        
    # load data from disk
    if conf['data_mode'] == 'preload':
        ldata, lpeaks, lpeakdata = dg.data_load(tsets, conf['dataset_dir'])
    else:
        ldata, lpeaks, lpeakdata = dg.data_load(tsets, out_path)
In [7]:
print("total number of peak numbers            : {0}".format(len(peak_numbers)))
print("total number of spectra per peak number : {0}".format(nsets))
print("total number of datasets per model      : {0}".format(tsets))
print("total number of inference runs          : {0}".format(nsets*len(peak_numbers)**2))
total number of peak numbers            : 4
total number of spectra per peak number : 4
total number of datasets per model      : 16
total number of inference runs          : 64
In [8]:
# plot datasets
fig.plot_datasets(ldata, lpeaks, dims=(int(tsets/2),2), figure_size=(12,int(tsets*(1.8))), 
                                                        savefig='yes', fname=file_basename, title='yes')

Initialize models and run inference

In [9]:
# convert pandas data to numpy arrays
x_val = np.array(xval, dtype='float32')

# store dataset y-values in list
cols = ldata[0].columns
y_val = [ldata[i][cols].values for i in range(len(ldata))]
In [10]:
# initialize models and run inference
models = []
traces = []

# list of tuples with (model,peak) combination
lmodpeak = []

# actual run number
run = 1

# total number of inference runs
truns = nsets * len(peak_numbers)**2

for pn in peak_numbers:
    if conf['model_mode'] == 'train':
        # for each model (from low-high peak number) run inference on all spectra
        print("running {0}-peak model".format(pn))
    
    for i in range(len(ldata)):
        if conf['peak_info'] == 'yes':
            # Get the peak numbers from the list. If the actual peak number in the spectrum is 
            # lower than what the model is expecting, then expand the list to the expected size,
            # duplicating the existing peak mu values, else truncate the list (taking the peaks
            # with the highest amplitude).
            plist = sorted(lpeaks[i])
            if len(plist) < pn:
                pl = sorted(np.resize(plist, (1,pn)).flatten())
            else:
                # sort peak info dataframe on amplitude value 
                l1 = lpeakdata[i].sort_values('amp', ascending=False)
                # truncate list to expected peak number
                pl = l1['mu'].values[:pn]
                
            model_g = mdl.model_gauss(xvalues=x_val, observations=y_val[i], npeaks=pn, 
                                      mu_peaks=pl, pmodel=conf['prior_model'])
        else:
            model_g = mdl.model_gauss(xvalues=x_val, observations=y_val[i], npeaks=pn,
                                      pmodel=conf['prior_model'])      
        models.append(model_g)

        with model_g:
            if conf['model_mode'] == 'train':
                print("({2}/{3}) running inference on dataset #{0}/{1} [{4}-peak model:{5}-peak spectrum]"
                      .format(i+1,len(ldata),run,truns,pn,len(plist)))
                lmodpeak += [(pn,len(plist))]
                trace_g = pm.sample(conf['nsamples'], init=conf['init_mode'], cores=conf['ncores'])
                traces.append(trace_g)
                # save inference results
                pm.backends.text.dump(out_path + '/traces_%02d' % (run), trace_g)
            else:
                # load traces from disk
                print("loading dataset #{0}/{1}".format(run,truns))
                trace_g = pm.backends.text.load(out_path + '/traces_%02d' % (run))
                traces.append(trace_g)
            run += 1
running 2-peak model
Auto-assigning NUTS sampler...
Initializing NUTS using adapt_diag...
(1/64) running inference on dataset #1/16 [2-peak model:2-peak spectrum]
Multiprocess sampling (2 chains in 2 jobs)
NUTS: [epsilon, sigma_e, sigma, mu, amp]
Sampling 2 chains, 2 divergences: 100%|██████████| 5000/5000 [00:15<00:00, 321.09draws/s]
There were 2 divergences after tuning. Increase `target_accept` or reparameterize.
Auto-assigning NUTS sampler...
Initializing NUTS using adapt_diag...
(2/64) running inference on dataset #2/16 [2-peak model:2-peak spectrum]
Multiprocess sampling (2 chains in 2 jobs)
NUTS: [epsilon, sigma_e, sigma, mu, amp]
Sampling 2 chains, 0 divergences: 100%|██████████| 5000/5000 [00:22<00:00, 222.46draws/s]
The acceptance probability does not match the target. It is 0.9142904363486067, but should be close to 0.8. Try to increase the number of tuning steps.
Auto-assigning NUTS sampler...
Initializing NUTS using adapt_diag...
(3/64) running inference on dataset #3/16 [2-peak model:2-peak spectrum]
Multiprocess sampling (2 chains in 2 jobs)
NUTS: [epsilon, sigma_e, sigma, mu, amp]
Sampling 2 chains, 0 divergences: 100%|██████████| 5000/5000 [00:14<00:00, 348.62draws/s]
The acceptance probability does not match the target. It is 0.8963214410889767, but should be close to 0.8. Try to increase the number of tuning steps.
Auto-assigning NUTS sampler...
Initializing NUTS using adapt_diag...
(4/64) running inference on dataset #4/16 [2-peak model:2-peak spectrum]
Multiprocess sampling (2 chains in 2 jobs)
NUTS: [epsilon, sigma_e, sigma, mu, amp]
Sampling 2 chains, 9 divergences: 100%|██████████| 5000/5000 [00:12<00:00, 411.98draws/s]
The acceptance probability does not match the target. It is 0.892183823503925, but should be close to 0.8. Try to increase the number of tuning steps.
There were 9 divergences after tuning. Increase `target_accept` or reparameterize.
Auto-assigning NUTS sampler...
Initializing NUTS using adapt_diag...
(5/64) running inference on dataset #5/16 [2-peak model:3-peak spectrum]
Multiprocess sampling (2 chains in 2 jobs)
NUTS: [epsilon, sigma_e, sigma, mu, amp]
Sampling 2 chains, 0 divergences: 100%|██████████| 5000/5000 [00:13<00:00, 364.92draws/s]
Auto-assigning NUTS sampler...
Initializing NUTS using adapt_diag...
(6/64) running inference on dataset #6/16 [2-peak model:3-peak spectrum]
Multiprocess sampling (2 chains in 2 jobs)
NUTS: [epsilon, sigma_e, sigma, mu, amp]
Sampling 2 chains, 1 divergences: 100%|██████████| 5000/5000 [00:11<00:00, 449.82draws/s]
There was 1 divergence after tuning. Increase `target_accept` or reparameterize.
The acceptance probability does not match the target. It is 0.8912114460778023, but should be close to 0.8. Try to increase the number of tuning steps.
The acceptance probability does not match the target. It is 0.9148163073320046, but should be close to 0.8. Try to increase the number of tuning steps.
Auto-assigning NUTS sampler...
Initializing NUTS using adapt_diag...
(7/64) running inference on dataset #7/16 [2-peak model:3-peak spectrum]
Multiprocess sampling (2 chains in 2 jobs)
NUTS: [epsilon, sigma_e, sigma, mu, amp]
Sampling 2 chains, 0 divergences: 100%|██████████| 5000/5000 [00:11<00:00, 442.24draws/s]
Auto-assigning NUTS sampler...
Initializing NUTS using adapt_diag...
(8/64) running inference on dataset #8/16 [2-peak model:3-peak spectrum]
Multiprocess sampling (2 chains in 2 jobs)
NUTS: [epsilon, sigma_e, sigma, mu, amp]
Sampling 2 chains, 0 divergences: 100%|██████████| 5000/5000 [00:10<00:00, 458.26draws/s]
The acceptance probability does not match the target. It is 0.8857134997868954, but should be close to 0.8. Try to increase the number of tuning steps.
Auto-assigning NUTS sampler...
Initializing NUTS using adapt_diag...
(9/64) running inference on dataset #9/16 [2-peak model:4-peak spectrum]
Multiprocess sampling (2 chains in 2 jobs)
NUTS: [epsilon, sigma_e, sigma, mu, amp]
Sampling 2 chains, 0 divergences: 100%|██████████| 5000/5000 [00:13<00:00, 362.40draws/s]
Auto-assigning NUTS sampler...
Initializing NUTS using adapt_diag...
(10/64) running inference on dataset #10/16 [2-peak model:4-peak spectrum]
Multiprocess sampling (2 chains in 2 jobs)
NUTS: [epsilon, sigma_e, sigma, mu, amp]
Sampling 2 chains, 0 divergences: 100%|██████████| 5000/5000 [00:08<00:00, 587.87draws/s]
Auto-assigning NUTS sampler...
Initializing NUTS using adapt_diag...
(11/64) running inference on dataset #11/16 [2-peak model:4-peak spectrum]
Multiprocess sampling (2 chains in 2 jobs)
NUTS: [epsilon, sigma_e, sigma, mu, amp]
Sampling 2 chains, 0 divergences: 100%|██████████| 5000/5000 [00:11<00:00, 445.95draws/s]
Auto-assigning NUTS sampler...
Initializing NUTS using adapt_diag...
(12/64) running inference on dataset #12/16 [2-peak model:4-peak spectrum]
Multiprocess sampling (2 chains in 2 jobs)
NUTS: [epsilon, sigma_e, sigma, mu, amp]
Sampling 2 chains, 0 divergences: 100%|██████████| 5000/5000 [00:11<00:00, 425.39draws/s]
Auto-assigning NUTS sampler...
Initializing NUTS using adapt_diag...
(13/64) running inference on dataset #13/16 [2-peak model:5-peak spectrum]
Multiprocess sampling (2 chains in 2 jobs)
NUTS: [epsilon, sigma_e, sigma, mu, amp]
Sampling 2 chains, 0 divergences: 100%|██████████| 5000/5000 [00:10<00:00, 482.77draws/s]
The acceptance probability does not match the target. It is 0.8848103668923332, but should be close to 0.8. Try to increase the number of tuning steps.
Auto-assigning NUTS sampler...
Initializing NUTS using adapt_diag...
(14/64) running inference on dataset #14/16 [2-peak model:5-peak spectrum]
Multiprocess sampling (2 chains in 2 jobs)
NUTS: [epsilon, sigma_e, sigma, mu, amp]
Sampling 2 chains, 0 divergences: 100%|██████████| 5000/5000 [00:12<00:00, 395.30draws/s]
The acceptance probability does not match the target. It is 0.8814041065098537, but should be close to 0.8. Try to increase the number of tuning steps.
The acceptance probability does not match the target. It is 0.8787249286203853, but should be close to 0.8. Try to increase the number of tuning steps.
Auto-assigning NUTS sampler...
Initializing NUTS using adapt_diag...
(15/64) running inference on dataset #15/16 [2-peak model:5-peak spectrum]
Multiprocess sampling (2 chains in 2 jobs)
NUTS: [epsilon, sigma_e, sigma, mu, amp]
Sampling 2 chains, 0 divergences: 100%|██████████| 5000/5000 [00:11<00:00, 450.56draws/s]
Auto-assigning NUTS sampler...
Initializing NUTS using adapt_diag...
(16/64) running inference on dataset #16/16 [2-peak model:5-peak spectrum]
Multiprocess sampling (2 chains in 2 jobs)
NUTS: [epsilon, sigma_e, sigma, mu, amp]
Sampling 2 chains, 0 divergences: 100%|██████████| 5000/5000 [00:11<00:00, 430.59draws/s]
The acceptance probability does not match the target. It is 0.8908519208654624, but should be close to 0.8. Try to increase the number of tuning steps.
running 3-peak model
Auto-assigning NUTS sampler...
Initializing NUTS using adapt_diag...
(17/64) running inference on dataset #1/16 [3-peak model:2-peak spectrum]
Multiprocess sampling (2 chains in 2 jobs)
NUTS: [epsilon, sigma_e, sigma, mu, amp]
Sampling 2 chains, 31 divergences: 100%|██████████| 5000/5000 [10:54<00:00,  7.64draws/s]
There were 17 divergences after tuning. Increase `target_accept` or reparameterize.
The acceptance probability does not match the target. It is 0.9144668504324036, but should be close to 0.8. Try to increase the number of tuning steps.
There were 14 divergences after tuning. Increase `target_accept` or reparameterize.
The acceptance probability does not match the target. It is 0.8901395176723241, but should be close to 0.8. Try to increase the number of tuning steps.
The chain reached the maximum tree depth. Increase max_treedepth, increase target_accept or reparameterize.
The rhat statistic is larger than 1.4 for some parameters. The sampler did not converge.
The estimated number of effective samples is smaller than 200 for some parameters.
Auto-assigning NUTS sampler...
Initializing NUTS using adapt_diag...
(18/64) running inference on dataset #2/16 [3-peak model:2-peak spectrum]
Multiprocess sampling (2 chains in 2 jobs)
NUTS: [epsilon, sigma_e, sigma, mu, amp]
Sampling 2 chains, 129 divergences: 100%|██████████| 5000/5000 [03:45<00:00, 22.16draws/s]
There were 79 divergences after tuning. Increase `target_accept` or reparameterize.
There were 50 divergences after tuning. Increase `target_accept` or reparameterize.
The rhat statistic is larger than 1.4 for some parameters. The sampler did not converge.
The estimated number of effective samples is smaller than 200 for some parameters.
Auto-assigning NUTS sampler...
Initializing NUTS using adapt_diag...
(19/64) running inference on dataset #3/16 [3-peak model:2-peak spectrum]
Multiprocess sampling (2 chains in 2 jobs)
NUTS: [epsilon, sigma_e, sigma, mu, amp]
Sampling 2 chains, 223 divergences: 100%|██████████| 5000/5000 [01:22<00:00, 60.45draws/s] 
There were 17 divergences after tuning. Increase `target_accept` or reparameterize.
The acceptance probability does not match the target. It is 0.9155835666595808, but should be close to 0.8. Try to increase the number of tuning steps.
There were 206 divergences after tuning. Increase `target_accept` or reparameterize.
The rhat statistic is larger than 1.2 for some parameters.
The estimated number of effective samples is smaller than 200 for some parameters.
Auto-assigning NUTS sampler...
Initializing NUTS using adapt_diag...
(20/64) running inference on dataset #4/16 [3-peak model:2-peak spectrum]
Multiprocess sampling (2 chains in 2 jobs)
NUTS: [epsilon, sigma_e, sigma, mu, amp]
Sampling 2 chains, 216 divergences: 100%|██████████| 5000/5000 [01:20<00:00, 62.42draws/s] 
There were 95 divergences after tuning. Increase `target_accept` or reparameterize.
There were 121 divergences after tuning. Increase `target_accept` or reparameterize.
The rhat statistic is larger than 1.2 for some parameters.
The estimated number of effective samples is smaller than 200 for some parameters.
Auto-assigning NUTS sampler...
Initializing NUTS using adapt_diag...
(21/64) running inference on dataset #5/16 [3-peak model:3-peak spectrum]
Multiprocess sampling (2 chains in 2 jobs)
NUTS: [epsilon, sigma_e, sigma, mu, amp]
Sampling 2 chains, 0 divergences: 100%|██████████| 5000/5000 [00:12<00:00, 407.22draws/s]
Auto-assigning NUTS sampler...
Initializing NUTS using adapt_diag...
(22/64) running inference on dataset #6/16 [3-peak model:3-peak spectrum]
Multiprocess sampling (2 chains in 2 jobs)
NUTS: [epsilon, sigma_e, sigma, mu, amp]
Sampling 2 chains, 0 divergences: 100%|██████████| 5000/5000 [05:30<00:00, 15.14draws/s]
The acceptance probability does not match the target. It is 0.8807880859618562, but should be close to 0.8. Try to increase the number of tuning steps.
The acceptance probability does not match the target. It is 0.8856563330311973, but should be close to 0.8. Try to increase the number of tuning steps.
Auto-assigning NUTS sampler...
Initializing NUTS using adapt_diag...
(23/64) running inference on dataset #7/16 [3-peak model:3-peak spectrum]
Multiprocess sampling (2 chains in 2 jobs)
NUTS: [epsilon, sigma_e, sigma, mu, amp]
Sampling 2 chains, 0 divergences: 100%|██████████| 5000/5000 [00:12<00:00, 385.92draws/s]
The acceptance probability does not match the target. It is 0.8794655494907649, but should be close to 0.8. Try to increase the number of tuning steps.
Auto-assigning NUTS sampler...
Initializing NUTS using adapt_diag...
(24/64) running inference on dataset #8/16 [3-peak model:3-peak spectrum]
Multiprocess sampling (2 chains in 2 jobs)
NUTS: [epsilon, sigma_e, sigma, mu, amp]
Sampling 2 chains, 0 divergences: 100%|██████████| 5000/5000 [00:34<00:00, 143.07draws/s]
The acceptance probability does not match the target. It is 0.9435164153343026, but should be close to 0.8. Try to increase the number of tuning steps.
The acceptance probability does not match the target. It is 0.9480816059068369, but should be close to 0.8. Try to increase the number of tuning steps.
Auto-assigning NUTS sampler...
Initializing NUTS using adapt_diag...
(25/64) running inference on dataset #9/16 [3-peak model:4-peak spectrum]
Multiprocess sampling (2 chains in 2 jobs)
NUTS: [epsilon, sigma_e, sigma, mu, amp]
Sampling 2 chains, 0 divergences: 100%|██████████| 5000/5000 [00:15<00:00, 326.70draws/s]
Auto-assigning NUTS sampler...
Initializing NUTS using adapt_diag...
(26/64) running inference on dataset #10/16 [3-peak model:4-peak spectrum]
Multiprocess sampling (2 chains in 2 jobs)
NUTS: [epsilon, sigma_e, sigma, mu, amp]
Sampling 2 chains, 0 divergences: 100%|██████████| 5000/5000 [00:16<00:00, 296.95draws/s]
Auto-assigning NUTS sampler...
Initializing NUTS using adapt_diag...
(27/64) running inference on dataset #11/16 [3-peak model:4-peak spectrum]
Multiprocess sampling (2 chains in 2 jobs)
NUTS: [epsilon, sigma_e, sigma, mu, amp]
Sampling 2 chains, 0 divergences: 100%|██████████| 5000/5000 [00:12<00:00, 405.94draws/s]
Auto-assigning NUTS sampler...
Initializing NUTS using adapt_diag...
(28/64) running inference on dataset #12/16 [3-peak model:4-peak spectrum]
Multiprocess sampling (2 chains in 2 jobs)
NUTS: [epsilon, sigma_e, sigma, mu, amp]
Sampling 2 chains, 1 divergences: 100%|██████████| 5000/5000 [00:12<00:00, 405.41draws/s]
There was 1 divergence after tuning. Increase `target_accept` or reparameterize.
Auto-assigning NUTS sampler...
Initializing NUTS using adapt_diag...
(29/64) running inference on dataset #13/16 [3-peak model:5-peak spectrum]
Multiprocess sampling (2 chains in 2 jobs)
NUTS: [epsilon, sigma_e, sigma, mu, amp]
Sampling 2 chains, 561 divergences: 100%|██████████| 5000/5000 [01:41<00:00, 49.02draws/s] 
The acceptance probability does not match the target. It is 0.890933709005887, but should be close to 0.8. Try to increase the number of tuning steps.
There were 561 divergences after tuning. Increase `target_accept` or reparameterize.
The acceptance probability does not match the target. It is 0.34410283523547003, but should be close to 0.8. Try to increase the number of tuning steps.
The rhat statistic is larger than 1.4 for some parameters. The sampler did not converge.
The estimated number of effective samples is smaller than 200 for some parameters.
Auto-assigning NUTS sampler...
Initializing NUTS using adapt_diag...
(30/64) running inference on dataset #14/16 [3-peak model:5-peak spectrum]
Multiprocess sampling (2 chains in 2 jobs)
NUTS: [epsilon, sigma_e, sigma, mu, amp]
Sampling 2 chains, 0 divergences: 100%|██████████| 5000/5000 [00:12<00:00, 407.56draws/s]
Auto-assigning NUTS sampler...
Initializing NUTS using adapt_diag...
(31/64) running inference on dataset #15/16 [3-peak model:5-peak spectrum]
Multiprocess sampling (2 chains in 2 jobs)
NUTS: [epsilon, sigma_e, sigma, mu, amp]
Sampling 2 chains, 5 divergences: 100%|██████████| 5000/5000 [00:10<00:00, 459.01draws/s]
There were 5 divergences after tuning. Increase `target_accept` or reparameterize.
Auto-assigning NUTS sampler...
Initializing NUTS using adapt_diag...
(32/64) running inference on dataset #16/16 [3-peak model:5-peak spectrum]
Multiprocess sampling (2 chains in 2 jobs)
NUTS: [epsilon, sigma_e, sigma, mu, amp]
Sampling 2 chains, 0 divergences: 100%|██████████| 5000/5000 [00:16<00:00, 307.71draws/s]
running 4-peak model
Auto-assigning NUTS sampler...
Initializing NUTS using adapt_diag...
(33/64) running inference on dataset #1/16 [4-peak model:2-peak spectrum]
Multiprocess sampling (2 chains in 2 jobs)
NUTS: [epsilon, sigma_e, sigma, mu, amp]
Sampling 2 chains, 234 divergences: 100%|██████████| 5000/5000 [11:20<00:00,  7.35draws/s]
There were 116 divergences after tuning. Increase `target_accept` or reparameterize.
The chain reached the maximum tree depth. Increase max_treedepth, increase target_accept or reparameterize.
There were 117 divergences after tuning. Increase `target_accept` or reparameterize.
The acceptance probability does not match the target. It is 0.9132534766363855, but should be close to 0.8. Try to increase the number of tuning steps.
The chain reached the maximum tree depth. Increase max_treedepth, increase target_accept or reparameterize.
The rhat statistic is larger than 1.4 for some parameters. The sampler did not converge.
The estimated number of effective samples is smaller than 200 for some parameters.
Auto-assigning NUTS sampler...
Initializing NUTS using adapt_diag...
(34/64) running inference on dataset #2/16 [4-peak model:2-peak spectrum]
Multiprocess sampling (2 chains in 2 jobs)
NUTS: [epsilon, sigma_e, sigma, mu, amp]
Sampling 2 chains, 218 divergences: 100%|██████████| 5000/5000 [11:55<00:00,  6.99draws/s]
There were 17 divergences after tuning. Increase `target_accept` or reparameterize.
The chain reached the maximum tree depth. Increase max_treedepth, increase target_accept or reparameterize.
There were 201 divergences after tuning. Increase `target_accept` or reparameterize.
The chain reached the maximum tree depth. Increase max_treedepth, increase target_accept or reparameterize.
The rhat statistic is larger than 1.4 for some parameters. The sampler did not converge.
The estimated number of effective samples is smaller than 200 for some parameters.
Auto-assigning NUTS sampler...
Initializing NUTS using adapt_diag...
(35/64) running inference on dataset #3/16 [4-peak model:2-peak spectrum]
Multiprocess sampling (2 chains in 2 jobs)
NUTS: [epsilon, sigma_e, sigma, mu, amp]
Sampling 2 chains, 157 divergences: 100%|██████████| 5000/5000 [02:05<00:00, 39.79draws/s]
There were 25 divergences after tuning. Increase `target_accept` or reparameterize.
There were 132 divergences after tuning. Increase `target_accept` or reparameterize.
The rhat statistic is larger than 1.4 for some parameters. The sampler did not converge.
The estimated number of effective samples is smaller than 200 for some parameters.
Auto-assigning NUTS sampler...
Initializing NUTS using adapt_diag...
(36/64) running inference on dataset #4/16 [4-peak model:2-peak spectrum]
Multiprocess sampling (2 chains in 2 jobs)
NUTS: [epsilon, sigma_e, sigma, mu, amp]
Sampling 2 chains, 1 divergences: 100%|██████████| 5000/5000 [03:15<00:00, 25.62draws/s]
The acceptance probability does not match the target. It is 0.9628711856726038, but should be close to 0.8. Try to increase the number of tuning steps.
There was 1 divergence after tuning. Increase `target_accept` or reparameterize.
The acceptance probability does not match the target. It is 0.9815938403285466, but should be close to 0.8. Try to increase the number of tuning steps.
The rhat statistic is larger than 1.4 for some parameters. The sampler did not converge.
The estimated number of effective samples is smaller than 200 for some parameters.
Auto-assigning NUTS sampler...
Initializing NUTS using adapt_diag...
(37/64) running inference on dataset #5/16 [4-peak model:3-peak spectrum]
Multiprocess sampling (2 chains in 2 jobs)
NUTS: [epsilon, sigma_e, sigma, mu, amp]
Sampling 2 chains, 2,813 divergences: 100%|██████████| 5000/5000 [01:08<00:00, 72.72draws/s] 
There were 1328 divergences after tuning. Increase `target_accept` or reparameterize.
The acceptance probability does not match the target. It is 0.13897741702532887, but should be close to 0.8. Try to increase the number of tuning steps.
There were 1485 divergences after tuning. Increase `target_accept` or reparameterize.
The acceptance probability does not match the target. It is 0.01357222326410735, but should be close to 0.8. Try to increase the number of tuning steps.
The rhat statistic is larger than 1.4 for some parameters. The sampler did not converge.
The estimated number of effective samples is smaller than 200 for some parameters.
Auto-assigning NUTS sampler...
Initializing NUTS using adapt_diag...
(38/64) running inference on dataset #6/16 [4-peak model:3-peak spectrum]
Multiprocess sampling (2 chains in 2 jobs)
NUTS: [epsilon, sigma_e, sigma, mu, amp]
Sampling 2 chains, 213 divergences: 100%|██████████| 5000/5000 [09:17<00:00,  8.97draws/s]
The acceptance probability does not match the target. It is 0.9438344174315154, but should be close to 0.8. Try to increase the number of tuning steps.
There were 213 divergences after tuning. Increase `target_accept` or reparameterize.
The chain reached the maximum tree depth. Increase max_treedepth, increase target_accept or reparameterize.
The rhat statistic is larger than 1.4 for some parameters. The sampler did not converge.
The estimated number of effective samples is smaller than 200 for some parameters.
Auto-assigning NUTS sampler...
Initializing NUTS using adapt_diag...
(39/64) running inference on dataset #7/16 [4-peak model:3-peak spectrum]
Multiprocess sampling (2 chains in 2 jobs)
NUTS: [epsilon, sigma_e, sigma, mu, amp]
Sampling 2 chains, 1,585 divergences: 100%|██████████| 5000/5000 [01:04<00:00, 76.97draws/s] 
There were 260 divergences after tuning. Increase `target_accept` or reparameterize.
The acceptance probability does not match the target. It is 0.6917757627855141, but should be close to 0.8. Try to increase the number of tuning steps.
There were 1325 divergences after tuning. Increase `target_accept` or reparameterize.
The acceptance probability does not match the target. It is 0.09693349828895369, but should be close to 0.8. Try to increase the number of tuning steps.
The rhat statistic is larger than 1.4 for some parameters. The sampler did not converge.
The estimated number of effective samples is smaller than 200 for some parameters.
Auto-assigning NUTS sampler...
Initializing NUTS using adapt_diag...
(40/64) running inference on dataset #8/16 [4-peak model:3-peak spectrum]
Multiprocess sampling (2 chains in 2 jobs)
NUTS: [epsilon, sigma_e, sigma, mu, amp]
Sampling 2 chains, 178 divergences: 100%|██████████| 5000/5000 [09:56<00:00,  8.38draws/s]
There were 177 divergences after tuning. Increase `target_accept` or reparameterize.
The chain reached the maximum tree depth. Increase max_treedepth, increase target_accept or reparameterize.
There was 1 divergence after tuning. Increase `target_accept` or reparameterize.
The acceptance probability does not match the target. It is 0.9431028055526276, but should be close to 0.8. Try to increase the number of tuning steps.
The rhat statistic is larger than 1.4 for some parameters. The sampler did not converge.
The estimated number of effective samples is smaller than 200 for some parameters.
Auto-assigning NUTS sampler...
Initializing NUTS using adapt_diag...
(41/64) running inference on dataset #9/16 [4-peak model:4-peak spectrum]
Multiprocess sampling (2 chains in 2 jobs)
NUTS: [epsilon, sigma_e, sigma, mu, amp]
Sampling 2 chains, 0 divergences: 100%|██████████| 5000/5000 [00:17<00:00, 282.30draws/s]
The acceptance probability does not match the target. It is 0.8786427785426383, but should be close to 0.8. Try to increase the number of tuning steps.
The acceptance probability does not match the target. It is 0.9011548609796566, but should be close to 0.8. Try to increase the number of tuning steps.
Auto-assigning NUTS sampler...
Initializing NUTS using adapt_diag...
(42/64) running inference on dataset #10/16 [4-peak model:4-peak spectrum]
Multiprocess sampling (2 chains in 2 jobs)
NUTS: [epsilon, sigma_e, sigma, mu, amp]
Sampling 2 chains, 0 divergences: 100%|██████████| 5000/5000 [00:21<00:00, 236.78draws/s]
The acceptance probability does not match the target. It is 0.9151121141584069, but should be close to 0.8. Try to increase the number of tuning steps.
The acceptance probability does not match the target. It is 0.880503465519684, but should be close to 0.8. Try to increase the number of tuning steps.
Auto-assigning NUTS sampler...
Initializing NUTS using adapt_diag...
(43/64) running inference on dataset #11/16 [4-peak model:4-peak spectrum]
Multiprocess sampling (2 chains in 2 jobs)
NUTS: [epsilon, sigma_e, sigma, mu, amp]
Sampling 2 chains, 0 divergences: 100%|██████████| 5000/5000 [00:17<00:00, 293.94draws/s]
The acceptance probability does not match the target. It is 0.9224785729909648, but should be close to 0.8. Try to increase the number of tuning steps.
The acceptance probability does not match the target. It is 0.8940729489537234, but should be close to 0.8. Try to increase the number of tuning steps.
Auto-assigning NUTS sampler...
Initializing NUTS using adapt_diag...
(44/64) running inference on dataset #12/16 [4-peak model:4-peak spectrum]
Multiprocess sampling (2 chains in 2 jobs)
NUTS: [epsilon, sigma_e, sigma, mu, amp]
Sampling 2 chains, 0 divergences: 100%|██████████| 5000/5000 [00:15<00:00, 313.74draws/s]
The acceptance probability does not match the target. It is 0.897146105312863, but should be close to 0.8. Try to increase the number of tuning steps.
The acceptance probability does not match the target. It is 0.8803654142877744, but should be close to 0.8. Try to increase the number of tuning steps.
Auto-assigning NUTS sampler...
Initializing NUTS using adapt_diag...
(45/64) running inference on dataset #13/16 [4-peak model:5-peak spectrum]
Multiprocess sampling (2 chains in 2 jobs)
NUTS: [epsilon, sigma_e, sigma, mu, amp]
Sampling 2 chains, 0 divergences: 100%|██████████| 5000/5000 [05:02<00:00, 16.55draws/s]
The number of effective samples is smaller than 25% for some parameters.
Auto-assigning NUTS sampler...
Initializing NUTS using adapt_diag...
(46/64) running inference on dataset #14/16 [4-peak model:5-peak spectrum]
Multiprocess sampling (2 chains in 2 jobs)
NUTS: [epsilon, sigma_e, sigma, mu, amp]
Sampling 2 chains, 0 divergences: 100%|██████████| 5000/5000 [00:15<00:00, 327.42draws/s]
Auto-assigning NUTS sampler...
Initializing NUTS using adapt_diag...
(47/64) running inference on dataset #15/16 [4-peak model:5-peak spectrum]
Multiprocess sampling (2 chains in 2 jobs)
NUTS: [epsilon, sigma_e, sigma, mu, amp]
Sampling 2 chains, 0 divergences: 100%|██████████| 5000/5000 [00:13<00:00, 378.97draws/s]
Auto-assigning NUTS sampler...
Initializing NUTS using adapt_diag...
(48/64) running inference on dataset #16/16 [4-peak model:5-peak spectrum]
Multiprocess sampling (2 chains in 2 jobs)
NUTS: [epsilon, sigma_e, sigma, mu, amp]
Sampling 2 chains, 0 divergences: 100%|██████████| 5000/5000 [00:14<00:00, 337.21draws/s]
The acceptance probability does not match the target. It is 0.8895595442819381, but should be close to 0.8. Try to increase the number of tuning steps.
running 5-peak model
Auto-assigning NUTS sampler...
Initializing NUTS using adapt_diag...
(49/64) running inference on dataset #1/16 [5-peak model:2-peak spectrum]
Multiprocess sampling (2 chains in 2 jobs)
NUTS: [epsilon, sigma_e, sigma, mu, amp]
Sampling 2 chains, 31 divergences: 100%|██████████| 5000/5000 [08:14<00:00, 10.12draws/s]
There were 15 divergences after tuning. Increase `target_accept` or reparameterize.
There were 16 divergences after tuning. Increase `target_accept` or reparameterize.
The acceptance probability does not match the target. It is 0.9812475183551602, but should be close to 0.8. Try to increase the number of tuning steps.
The chain reached the maximum tree depth. Increase max_treedepth, increase target_accept or reparameterize.
The rhat statistic is larger than 1.4 for some parameters. The sampler did not converge.
The estimated number of effective samples is smaller than 200 for some parameters.
Auto-assigning NUTS sampler...
Initializing NUTS using adapt_diag...
(50/64) running inference on dataset #2/16 [5-peak model:2-peak spectrum]
Multiprocess sampling (2 chains in 2 jobs)
NUTS: [epsilon, sigma_e, sigma, mu, amp]
Sampling 2 chains, 1,838 divergences: 100%|██████████| 5000/5000 [11:48<00:00,  7.06draws/s] 
There were 79 divergences after tuning. Increase `target_accept` or reparameterize.
The chain reached the maximum tree depth. Increase max_treedepth, increase target_accept or reparameterize.
There were 1759 divergences after tuning. Increase `target_accept` or reparameterize.
The acceptance probability does not match the target. It is 0.20670907061759616, but should be close to 0.8. Try to increase the number of tuning steps.
The chain reached the maximum tree depth. Increase max_treedepth, increase target_accept or reparameterize.
The rhat statistic is larger than 1.4 for some parameters. The sampler did not converge.
The estimated number of effective samples is smaller than 200 for some parameters.
Auto-assigning NUTS sampler...
Initializing NUTS using adapt_diag...
(51/64) running inference on dataset #3/16 [5-peak model:2-peak spectrum]
Multiprocess sampling (2 chains in 2 jobs)
NUTS: [epsilon, sigma_e, sigma, mu, amp]
Sampling 2 chains, 91 divergences: 100%|██████████| 5000/5000 [11:34<00:00,  7.20draws/s]
There were 35 divergences after tuning. Increase `target_accept` or reparameterize.
The acceptance probability does not match the target. It is 0.9186635295416379, but should be close to 0.8. Try to increase the number of tuning steps.
The chain reached the maximum tree depth. Increase max_treedepth, increase target_accept or reparameterize.
There were 56 divergences after tuning. Increase `target_accept` or reparameterize.
The rhat statistic is larger than 1.4 for some parameters. The sampler did not converge.
The estimated number of effective samples is smaller than 200 for some parameters.
Auto-assigning NUTS sampler...
Initializing NUTS using adapt_diag...
(52/64) running inference on dataset #4/16 [5-peak model:2-peak spectrum]
Multiprocess sampling (2 chains in 2 jobs)
NUTS: [epsilon, sigma_e, sigma, mu, amp]
Sampling 2 chains, 197 divergences: 100%|██████████| 5000/5000 [11:41<00:00,  7.12draws/s]
There were 196 divergences after tuning. Increase `target_accept` or reparameterize.
The chain reached the maximum tree depth. Increase max_treedepth, increase target_accept or reparameterize.
There was 1 divergence after tuning. Increase `target_accept` or reparameterize.
The acceptance probability does not match the target. It is 0.9612763331080886, but should be close to 0.8. Try to increase the number of tuning steps.
The rhat statistic is larger than 1.4 for some parameters. The sampler did not converge.
The estimated number of effective samples is smaller than 200 for some parameters.
Auto-assigning NUTS sampler...
Initializing NUTS using adapt_diag...
(53/64) running inference on dataset #5/16 [5-peak model:3-peak spectrum]
Multiprocess sampling (2 chains in 2 jobs)
NUTS: [epsilon, sigma_e, sigma, mu, amp]
Sampling 2 chains, 588 divergences: 100%|██████████| 5000/5000 [11:46<00:00,  7.08draws/s]
There were 443 divergences after tuning. Increase `target_accept` or reparameterize.
There were 145 divergences after tuning. Increase `target_accept` or reparameterize.
The chain reached the maximum tree depth. Increase max_treedepth, increase target_accept or reparameterize.
The rhat statistic is larger than 1.4 for some parameters. The sampler did not converge.
The estimated number of effective samples is smaller than 200 for some parameters.
Auto-assigning NUTS sampler...
Initializing NUTS using adapt_diag...
(54/64) running inference on dataset #6/16 [5-peak model:3-peak spectrum]
Multiprocess sampling (2 chains in 2 jobs)
NUTS: [epsilon, sigma_e, sigma, mu, amp]
Sampling 2 chains, 257 divergences: 100%|██████████| 5000/5000 [09:28<00:00,  8.80draws/s]
There were 172 divergences after tuning. Increase `target_accept` or reparameterize.
There were 85 divergences after tuning. Increase `target_accept` or reparameterize.
The chain reached the maximum tree depth. Increase max_treedepth, increase target_accept or reparameterize.
The rhat statistic is larger than 1.4 for some parameters. The sampler did not converge.
The estimated number of effective samples is smaller than 200 for some parameters.
Auto-assigning NUTS sampler...
Initializing NUTS using adapt_diag...
(55/64) running inference on dataset #7/16 [5-peak model:3-peak spectrum]
Multiprocess sampling (2 chains in 2 jobs)
NUTS: [epsilon, sigma_e, sigma, mu, amp]
Sampling 2 chains, 1,549 divergences: 100%|██████████| 5000/5000 [03:04<00:00, 27.07draws/s] 
There were 334 divergences after tuning. Increase `target_accept` or reparameterize.
There were 1214 divergences after tuning. Increase `target_accept` or reparameterize.
The acceptance probability does not match the target. It is 0.20326993123957784, but should be close to 0.8. Try to increase the number of tuning steps.
The rhat statistic is larger than 1.4 for some parameters. The sampler did not converge.
The estimated number of effective samples is smaller than 200 for some parameters.
Auto-assigning NUTS sampler...
Initializing NUTS using adapt_diag...
(56/64) running inference on dataset #8/16 [5-peak model:3-peak spectrum]
Multiprocess sampling (2 chains in 2 jobs)
NUTS: [epsilon, sigma_e, sigma, mu, amp]
Sampling 2 chains, 1,864 divergences: 100%|██████████| 5000/5000 [11:32<00:00,  7.22draws/s] 
There were 1510 divergences after tuning. Increase `target_accept` or reparameterize.
The acceptance probability does not match the target. It is 0.15269227722390977, but should be close to 0.8. Try to increase the number of tuning steps.
The chain reached the maximum tree depth. Increase max_treedepth, increase target_accept or reparameterize.
There were 353 divergences after tuning. Increase `target_accept` or reparameterize.
The chain reached the maximum tree depth. Increase max_treedepth, increase target_accept or reparameterize.
The rhat statistic is larger than 1.4 for some parameters. The sampler did not converge.
The estimated number of effective samples is smaller than 200 for some parameters.
Auto-assigning NUTS sampler...
Initializing NUTS using adapt_diag...
(57/64) running inference on dataset #9/16 [5-peak model:4-peak spectrum]
Multiprocess sampling (2 chains in 2 jobs)
NUTS: [epsilon, sigma_e, sigma, mu, amp]
Sampling 2 chains, 1,180 divergences: 100%|██████████| 5000/5000 [08:14<00:00, 10.10draws/s] 
There were 1179 divergences after tuning. Increase `target_accept` or reparameterize.
The acceptance probability does not match the target. It is 0.22591505195895029, but should be close to 0.8. Try to increase the number of tuning steps.
The acceptance probability does not match the target. It is 0.9662509080676828, but should be close to 0.8. Try to increase the number of tuning steps.
The rhat statistic is larger than 1.4 for some parameters. The sampler did not converge.
The estimated number of effective samples is smaller than 200 for some parameters.
Auto-assigning NUTS sampler...
Initializing NUTS using adapt_diag...
(58/64) running inference on dataset #10/16 [5-peak model:4-peak spectrum]
Multiprocess sampling (2 chains in 2 jobs)
NUTS: [epsilon, sigma_e, sigma, mu, amp]
Sampling 2 chains, 10 divergences: 100%|██████████| 5000/5000 [07:43<00:00, 10.78draws/s]
The acceptance probability does not match the target. It is 0.9705230263206059, but should be close to 0.8. Try to increase the number of tuning steps.
There were 10 divergences after tuning. Increase `target_accept` or reparameterize.
The acceptance probability does not match the target. It is 0.9539910617484895, but should be close to 0.8. Try to increase the number of tuning steps.
The chain reached the maximum tree depth. Increase max_treedepth, increase target_accept or reparameterize.
The rhat statistic is larger than 1.4 for some parameters. The sampler did not converge.
The estimated number of effective samples is smaller than 200 for some parameters.
Auto-assigning NUTS sampler...
Initializing NUTS using adapt_diag...
(59/64) running inference on dataset #11/16 [5-peak model:4-peak spectrum]
Multiprocess sampling (2 chains in 2 jobs)
NUTS: [epsilon, sigma_e, sigma, mu, amp]
Sampling 2 chains, 548 divergences: 100%|██████████| 5000/5000 [10:30<00:00,  7.93draws/s]
There were 322 divergences after tuning. Increase `target_accept` or reparameterize.
The acceptance probability does not match the target. It is 0.8807838087746255, but should be close to 0.8. Try to increase the number of tuning steps.
There were 226 divergences after tuning. Increase `target_accept` or reparameterize.
The rhat statistic is larger than 1.4 for some parameters. The sampler did not converge.
The estimated number of effective samples is smaller than 200 for some parameters.
Auto-assigning NUTS sampler...
Initializing NUTS using adapt_diag...
(60/64) running inference on dataset #12/16 [5-peak model:4-peak spectrum]
Multiprocess sampling (2 chains in 2 jobs)
NUTS: [epsilon, sigma_e, sigma, mu, amp]
Sampling 2 chains, 9 divergences: 100%|██████████| 5000/5000 [03:42<00:00, 22.42draws/s]
There were 5 divergences after tuning. Increase `target_accept` or reparameterize.
The acceptance probability does not match the target. It is 0.8798287496152933, but should be close to 0.8. Try to increase the number of tuning steps.
There were 4 divergences after tuning. Increase `target_accept` or reparameterize.
The acceptance probability does not match the target. It is 0.9001835458180185, but should be close to 0.8. Try to increase the number of tuning steps.
The number of effective samples is smaller than 25% for some parameters.
Auto-assigning NUTS sampler...
Initializing NUTS using adapt_diag...
(61/64) running inference on dataset #13/16 [5-peak model:5-peak spectrum]
Multiprocess sampling (2 chains in 2 jobs)
NUTS: [epsilon, sigma_e, sigma, mu, amp]
Sampling 2 chains, 0 divergences: 100%|██████████| 5000/5000 [09:39<00:00,  8.64draws/s]
The acceptance probability does not match the target. It is 0.9117463533163787, but should be close to 0.8. Try to increase the number of tuning steps.
The chain reached the maximum tree depth. Increase max_treedepth, increase target_accept or reparameterize.
The chain reached the maximum tree depth. Increase max_treedepth, increase target_accept or reparameterize.
Auto-assigning NUTS sampler...
Initializing NUTS using adapt_diag...
(62/64) running inference on dataset #14/16 [5-peak model:5-peak spectrum]
Multiprocess sampling (2 chains in 2 jobs)
NUTS: [epsilon, sigma_e, sigma, mu, amp]
Sampling 2 chains, 1 divergences: 100%|██████████| 5000/5000 [00:45<00:00, 110.68draws/s]
The acceptance probability does not match the target. It is 0.9355658491782621, but should be close to 0.8. Try to increase the number of tuning steps.
The acceptance probability does not match the target. It is 0.9387582667681902, but should be close to 0.8. Try to increase the number of tuning steps.
Auto-assigning NUTS sampler...
Initializing NUTS using adapt_diag...
(63/64) running inference on dataset #15/16 [5-peak model:5-peak spectrum]
Multiprocess sampling (2 chains in 2 jobs)
NUTS: [epsilon, sigma_e, sigma, mu, amp]
Sampling 2 chains, 4 divergences: 100%|██████████| 5000/5000 [00:15<00:00, 329.21draws/s]
There was 1 divergence after tuning. Increase `target_accept` or reparameterize.
There were 3 divergences after tuning. Increase `target_accept` or reparameterize.
Auto-assigning NUTS sampler...
Initializing NUTS using adapt_diag...
(64/64) running inference on dataset #16/16 [5-peak model:5-peak spectrum]
Multiprocess sampling (2 chains in 2 jobs)
NUTS: [epsilon, sigma_e, sigma, mu, amp]
Sampling 2 chains, 4 divergences: 100%|██████████| 5000/5000 [00:16<00:00, 294.41draws/s]
There were 3 divergences after tuning. Increase `target_accept` or reparameterize.
There was 1 divergence after tuning. Increase `target_accept` or reparameterize.

Model visualization

In [11]:
pm.model_to_graphviz(models[0])
Out[11]:
%3 cluster1 x 2 1 x 2 cluster100 100 cluster15 x 100 15 x 100 sigma sigma ~ Lognormal y y ~ Deterministic sigma->y mu mu ~ Uniform mu->y amp amp ~ Uniform amp->y y_pred y_pred ~ Normal y->y_pred epsilon epsilon ~ HalfNormal epsilon->y_pred sigma_e sigma_e ~ Gamma sigma_e->epsilon
In [12]:
# save model figure as image
img = pm.model_to_graphviz(models[0])
img.render(filename=file_basename + '_model', format='png');

Collect results and save

In [13]:
# posterior predictive traces
ppc = [pm.sample_posterior_predictive(traces[i], samples=500, model=models[i]) for i in range(len(traces))]
/home/johan/VirtualEnv/ppsda/lib/python3.6/site-packages/pymc3/sampling.py:1247: UserWarning: samples parameter is smaller than nchains times ndraws, some draws and/or chains may not be represented in the returned posterior predictive sample
  "samples parameter is smaller than nchains times ndraws, some draws "
100%|██████████| 500/500 [00:01<00:00, 445.38it/s]
/home/johan/VirtualEnv/ppsda/lib/python3.6/site-packages/pymc3/sampling.py:1247: UserWarning: samples parameter is smaller than nchains times ndraws, some draws and/or chains may not be represented in the returned posterior predictive sample
  "samples parameter is smaller than nchains times ndraws, some draws "
100%|██████████| 500/500 [00:01<00:00, 457.85it/s]
100%|██████████| 500/500 [00:01<00:00, 458.54it/s]
100%|██████████| 500/500 [00:01<00:00, 468.20it/s]
100%|██████████| 500/500 [00:01<00:00, 464.17it/s]
100%|██████████| 500/500 [00:01<00:00, 467.13it/s]
100%|██████████| 500/500 [00:01<00:00, 467.38it/s]
100%|██████████| 500/500 [00:01<00:00, 463.44it/s]
100%|██████████| 500/500 [00:01<00:00, 475.98it/s]
100%|██████████| 500/500 [00:01<00:00, 473.40it/s]
100%|██████████| 500/500 [00:01<00:00, 457.10it/s]
100%|██████████| 500/500 [00:01<00:00, 457.08it/s]
100%|██████████| 500/500 [00:01<00:00, 465.70it/s]
100%|██████████| 500/500 [00:01<00:00, 457.68it/s]
100%|██████████| 500/500 [00:01<00:00, 461.63it/s]
100%|██████████| 500/500 [00:01<00:00, 462.96it/s]
100%|██████████| 500/500 [00:01<00:00, 456.87it/s]
100%|██████████| 500/500 [00:01<00:00, 458.28it/s]
100%|██████████| 500/500 [00:01<00:00, 460.75it/s]
100%|██████████| 500/500 [00:01<00:00, 455.00it/s]
100%|██████████| 500/500 [00:01<00:00, 441.85it/s]
100%|██████████| 500/500 [00:01<00:00, 459.09it/s]
100%|██████████| 500/500 [00:01<00:00, 457.97it/s]
100%|██████████| 500/500 [00:01<00:00, 463.02it/s]
100%|██████████| 500/500 [00:01<00:00, 455.21it/s]
100%|██████████| 500/500 [00:01<00:00, 461.89it/s]
100%|██████████| 500/500 [00:01<00:00, 459.80it/s]
100%|██████████| 500/500 [00:01<00:00, 465.31it/s]
100%|██████████| 500/500 [00:01<00:00, 453.48it/s]
100%|██████████| 500/500 [00:01<00:00, 446.37it/s]
100%|██████████| 500/500 [00:01<00:00, 458.55it/s]
100%|██████████| 500/500 [00:01<00:00, 472.09it/s]
100%|██████████| 500/500 [00:01<00:00, 461.92it/s]
100%|██████████| 500/500 [00:01<00:00, 461.40it/s]
100%|██████████| 500/500 [00:01<00:00, 462.69it/s]
100%|██████████| 500/500 [00:01<00:00, 462.86it/s]
100%|██████████| 500/500 [00:01<00:00, 460.95it/s]
100%|██████████| 500/500 [00:03<00:00, 164.41it/s]
100%|██████████| 500/500 [00:01<00:00, 459.47it/s]
100%|██████████| 500/500 [00:01<00:00, 458.12it/s]
100%|██████████| 500/500 [00:01<00:00, 448.75it/s]
100%|██████████| 500/500 [00:01<00:00, 455.25it/s]
100%|██████████| 500/500 [00:01<00:00, 457.76it/s]
100%|██████████| 500/500 [00:01<00:00, 455.08it/s]
100%|██████████| 500/500 [00:01<00:00, 450.42it/s]
100%|██████████| 500/500 [00:01<00:00, 445.13it/s]
100%|██████████| 500/500 [00:01<00:00, 456.06it/s]
100%|██████████| 500/500 [00:01<00:00, 462.00it/s]
100%|██████████| 500/500 [00:01<00:00, 459.29it/s]
100%|██████████| 500/500 [00:01<00:00, 461.60it/s]
100%|██████████| 500/500 [00:01<00:00, 440.55it/s]
100%|██████████| 500/500 [00:01<00:00, 452.05it/s]
100%|██████████| 500/500 [00:01<00:00, 452.66it/s]
100%|██████████| 500/500 [00:01<00:00, 456.36it/s]
100%|██████████| 500/500 [00:01<00:00, 450.28it/s]
100%|██████████| 500/500 [00:01<00:00, 459.53it/s]
100%|██████████| 500/500 [00:01<00:00, 439.88it/s]
100%|██████████| 500/500 [00:01<00:00, 444.11it/s]
100%|██████████| 500/500 [00:01<00:00, 453.47it/s]
100%|██████████| 500/500 [00:01<00:00, 438.55it/s]
100%|██████████| 500/500 [00:01<00:00, 441.24it/s]
100%|██████████| 500/500 [00:01<00:00, 447.76it/s]
100%|██████████| 500/500 [00:01<00:00, 440.07it/s]
100%|██████████| 500/500 [00:01<00:00, 436.46it/s]
In [14]:
# various plots to inspect the inference results
varnames = ['amp', 'mu', 'sigma', 'epsilon']

#az.plot_trace(traces[2], varnames, compact=True);
#az.plot_trace(traces[2], varnames, divergences='top');
#az.plot_autocorr(traces[0], varnames);
#az.plot_posterior(traces[2], varnames);

#for idx, trace in enumerate(traces):
#    az.plot_forest(trace, var_names = varnames, r_hat=True, ess=True);
In [15]:
if conf['model_mode'] == 'train':
    # collect the results and display
    df = res.get_results_summary(varnames, traces, ppc, y_val, epsilon_real=0.05, sets=tsets, labels=lmodpeak)
else:
    # load results from disk
    df = pd.read_csv(file_basename + '.csv')
    df.index += 1
#df.sort_values(by=['r2'])
df
/home/johan/VirtualEnv/ppsda/lib/python3.6/site-packages/arviz/stats/stats.py:1126: UserWarning: For one or more samples the posterior variance of the log predictive densities exceeds 0.4. This could be indication of WAIC starting to fail. 
See http://arxiv.org/abs/1507.04544 for details
  "For one or more samples the posterior variance of the log predictive "
Out[15]:
r_hat mcse ess bfmi r2 waic epsilon epsilon_real model peaks
1 1.0 0.000000 3010.428571 1.006182 0.999450 -4893.273272 0.047243 0.05 2 2
2 1.0 0.000143 1702.000000 1.106768 0.999906 -4751.137145 0.049541 0.05 2 2
3 1.0 0.000143 3065.428571 1.091763 0.999467 -4715.218509 0.050139 0.05 2 2
4 1.0 0.000000 5394.428571 1.113197 0.999571 -4709.271287 0.050253 0.05 2 2
5 1.0 0.002571 3513.714286 0.981846 0.846804 6496.999979 2.098160 0.05 2 3
... ... ... ... ... ... ... ... ... ... ...
60 1.0 0.015000 2591.625000 0.910526 0.999906 -4703.622301 0.050205 0.05 5 4
61 1.0 0.005125 1660.250000 1.012125 0.999945 -4794.810619 0.048662 0.05 5 5
62 1.0 0.000125 3910.750000 1.006410 0.999940 -4627.282302 0.051508 0.05 5 5
63 1.0 0.000000 5851.125000 1.016807 0.999548 -2238.847242 0.113962 0.05 5 5
64 1.0 0.000000 5017.750000 1.075812 0.999580 -2561.481152 0.101984 0.05 5 5

64 rows × 10 columns

In [16]:
if conf['model_mode'] == 'train':
    # save results to .csv
    df.to_csv(file_basename + '.csv', index=False)

Plot posterior

In [17]:
fig.plot_posterior(x_val, ldata, traces, ppc, dims=(int(truns/2),2), figure_size=(12,int(truns*(1.8))),
                    savefig='yes', fname=file_basename, showpeaks='no', sets=tsets, labels=lmodpeak)
In [18]:
fig.plot_posterior(x_val, ldata, traces, ppc, dims=(int(truns/2),2), figure_size=(12,int(truns*(1.8))),
                    savefig='yes', fname=file_basename, showpeaks='yes', sets=tsets, labels=lmodpeak)
In [19]:
cnf.close(out_path)